﻿using System;
using System.Reflection;
using System.Windows;
using Vampirewal.Core.SimpleMVVM;

namespace V.CodeGenerator.Test
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            //此处调用一下，方便初次打开系统的时候，创建数据库
            //using (DataContext dbContext = new DataContext())
            //{
            //    dbContext.Database.EnsureCreated();
            //}

            
            
        }


        /// <summary>
        /// 注册Messenger
        /// </summary>
        private void RegisterMessenger()
        {
            Messenger.Default.Register<string, FrameworkElement>(this, "GetView", GetView);
            Messenger.Default.Register<string>(this, "OpenWinodw", OpenWinodw);
            Messenger.Default.Register<string, object, object>(this, "OpenDialogWindowGetResultByPassData", OpenDialogWindowGetResultByPassData);
            Messenger.Default.Register<string, object>(this, "OpenDialogWindowGetResult", OpenDialogWindowGetResult);
            Messenger.Default.Register<string, bool>(this, "OpenShowDialogWindow", OpenShowDialogWindow);

        }

        /// <summary>
        /// 通过反射获取页面，并注册消息供ViewModel调用
        /// </summary>
        /// <param name="ViewName"></param>
        /// <returns></returns>
        private FrameworkElement GetView(string ViewName)
        {
            Type type = Type.GetType($"V.CodeGenerator.Test.{ViewName}");
            ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
            var current = (FrameworkElement)cti.Invoke(null);

            //this.MainContent = current;
            return current;
        }

        private void OpenWinodw(string ViewName)
        {
            Window w = GetView(ViewName) as Window;

            if (w != null)
            {
                //WindowsManager.CreateDialogWindowByViewModelResult(w,)
                w.Show();
            }
        }

        /// <summary>
        /// 开启DialogWindow窗体，传入值并获取返回值
        /// </summary>
        /// <param name="ViewName">窗体名称</param>
        /// <param name="param">传入参数</param>
        /// <returns>返回object</returns>
        private object OpenDialogWindowGetResultByPassData(string ViewName, object param)
        {
            Window w = GetView(ViewName) as Window;
            ViewModelBase vm = w.DataContext as ViewModelBase;
            vm.View = w;

            vm.PassData(param);
            w.ShowDialog();

            return vm.GetResult();
        }

        /// <summary>
        /// 开启DialogWindow窗体，传入值并获取返回值
        /// </summary>
        /// <param name="ViewName">窗体名称</param>
        /// <param name="param">传入参数</param>
        /// <returns>返回object</returns>
        private object OpenDialogWindowGetResult(string ViewName)
        {
            Window w = GetView(ViewName) as Window;
            ViewModelBase vm = w.DataContext as ViewModelBase;
            vm.View = w;

            w.ShowDialog();

            return vm.GetResult();
        }

        /// <summary>
        /// 开启DialogWindos窗体，返回true/false
        /// </summary>
        /// <param name="ViewName">窗体名称</param>
        /// <returns></returns>
        private bool OpenShowDialogWindow(string ViewName)
        {
            Window w = GetView(ViewName) as Window;
            ViewModelBase vm = w.DataContext as ViewModelBase;
            vm.View = w;
            if (w.ShowDialog() == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
